/**
 * show(key)
 * key 是控件的值  或者是页面的值
 *
 */
define(["utils", "base"],
function(utils, baseClass) {
  var viewPagerItem = function(config) {
    viewPagerItem.baseConstructor.call(this, config);
    this.$el.addClass("displayflex yy-viewpager-item");
    this.$el.append(config.contentInstance.$el);
  }

  utils.extends(viewPagerItem, baseClass);

  viewPagerItem.prototype.hide = function(){
    utils.css(this.$el,{left:"-100%"});
  }

  viewPagerItem.prototype.show = function(){
    utils.css(this.$el,{left:"0"});
  }
  function Component(config) {
    var _this = this;
    Component.baseConstructor.call(this, config);
    this.$el.addClass("yy-viewpager");
    this.items = {};
    var defaultKey = config.defaultKey;
    if(!defaultKey){
      console.error("viewpager控件为提供defalutKey属性");
    }
    this.curPageViewItem = null;
    this._getViewPagerItem(defaultKey,function(pvItem){
      _this.curPageViewItem = pvItem;
    },function(){

    });
  }

  utils.extends(Component, baseClass);

  Component.prototype._createViewPagerItem = function(key, type, contentInstance, success) {
    var vpi = new viewPagerItem({
      type: "viewpageritem",
      contentType: type,
      contentInstance: contentInstance,
      $$pageview: this.pageview,
      $$datasource: this.datasource,
      $$parent: this
    });
    this.items[key] = vpi;
    this.$el.append(vpi.$el);
    success(vpi);
  }

  Component.prototype.showItem = function(key) {
    //key可以是页面的Key 或者是控件的Key
    var _this = this;
    if(this.curPageViewItem){
      this.curPageViewItem.hide();
    }
    this._getViewPagerItem(key,function(pvItem){
      _this.curPageViewItem = pvItem;
      _this.curPageViewItem.show();
    },function(){

    });

  }



  Component.prototype._getViewPagerItem = function(key, success, error) {
    var _this = this;
    var item = this.items[key];
    if (item) {
      success(item);
      return;
    }
    key = this.config[key] || key; // 如果想要显示相同的两个页面 那么这个key就是唯一的值可作为Key
    var com_config = this.pageview.config.components[key];
    if (!com_config) {
      this.pageview.pageManager.getPageConfigByPageKey(key,
      function(pageConfig) {
        var page_instance = _this.pageview.pageManager.createPageView(pageConfig);
        _this._createViewPagerItem(key, "pageview", page_instance, success)
      },
      function() {
        error && error();
      })
    } else {
      this.getComponentInstanceByComKey(key, this.datasource,this.rowInstance,
      function(com_instance) {
        _this._createViewPagerItem(key, "component", com_instance, success)
      },
      function() {
        error && error();
      })
    }

  }

  return Component;
});
/**
 * Created by xiaoz on 16/8/13.
 */
